package com.example.smart_city_0829.activity.commonweal;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.commonweal.CommonwealRecommendList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class CommonwealSearchActivity extends BaseActivity {

    private String key = "";
    private ListView lv;

    private ListAdapter listAdapter;
    private List<CommonwealRecommendList.RowsBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_commonweal_search;
    }

    @Override
    protected void initView() {
        lv = (ListView) findViewById(R.id.lv);

    }

    @Override
    protected void initData() {
        key = getIntent().getStringExtra("key");

        api.getWelfareListOfName(key).enqueue(new NetCallBack<CommonwealRecommendList>() {
            @Override
            public void onResponse(Call<CommonwealRecommendList> call, Response<CommonwealRecommendList> response) {
                if (response.body() == null || response.body().getRows() == null) return;
                data = response.body().getRows();
                if (listAdapter == null){
                    listAdapter = new ListAdapter(data,CommonwealSearchActivity.this);
                    lv.setAdapter(listAdapter);
                }else {
                    listAdapter.resetData(data);
                    listAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected String getActionBarTitle() {
        return "搜索结果";
    }

    private final class ListAdapter extends CommonAdapter<CommonwealRecommendList.RowsBean,LVViewHolder> {

        public ListAdapter(List<CommonwealRecommendList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(CommonwealRecommendList.RowsBean data, int position, LVViewHolder viewHolder, View convertView, Context context) {
            Glide.with(CommonwealSearchActivity.this)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.title.setText(data.getName());
            viewHolder.content.setText("参与人数："+data.getDonateCount()+"\n"+"已筹善款："+data.getMoneyTotal());
            viewHolder.tag1.setText("公益类别："+data.getType());
            viewHolder.tag2.setText("发布组织："+data.getAuthor());
        }

        @Override
        protected LVViewHolder getViewHolder() {
            return new LVViewHolder();
        }

        @Override
        protected void initView(LVViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }
    }

    private static class LVViewHolder{
        ImageView im;
        TextView title;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }
}